package com.tjp.eight.ui.fragment.comment;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.otherhomepage.OtherHomePageAdapter;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.CircleImageView;
import com.tjp.eight.views.emoj.EmojiUtil;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/24.
 */
public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ArrayList<CommentModel> commentModels;
    private ItemZanClick itemZanClick;
    private OnItemClickListener onItemClickListener;
    private NameOnClick nameOnClick;
    private SubItemRepeat subItemRepeat;

    public CommentAdapter(Context context) {
        this.context = context;
        commentModels = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ((CommentViewHolder) holder).linearLayout.removeAllViews();
        ((CommentViewHolder) holder).rl_more_com.setVisibility(View.GONE);
        if (commentModels.get(position).child != null) {
            for (int i = 0; i < commentModels.get(position).child.size(); i++) {
                if (i > 4) {
                    break;
                }
                if (i >= 4) {
                    ((CommentViewHolder) holder).rl_more_com.setVisibility(View.VISIBLE);
                }
                View subView = LayoutInflater.from(context).inflate(R.layout.item_comment_sub, null);

                CircleImageView civ_u_pic_hot_comment = (CircleImageView) subView.findViewById(R.id.civ_u_pic_hot_comment);
                TextView tv_detail_comment_name = (TextView) subView.findViewById(R.id.tv_detail_comment_name);
                TextView tv_child_comment_content = (TextView) subView.findViewById(R.id.tv_child_comment_content);

                Glide.with(context).load(commentModels.get(position).child.get(i).iconurl).error(R.drawable.icon_login).into(civ_u_pic_hot_comment);
                final String uid = commentModels.get(position).child.get(i).uid;
                final String name = commentModels.get(position).child.get(i).name;
                subView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //回复该用户，如果是自己不给于回复
                        if (IsSuccessUtils.isMySelf(uid)) return;
                        if (subItemRepeat != null) {
                            subItemRepeat.repeat(commentModels.get(position).coid, position, name);
                        }
                    }
                });
                civ_u_pic_hot_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击头像，进入该用户详情
                        ActivityUtils.startOtherHomePage(context, uid);
                    }
                });
                tv_detail_comment_name.setText("" + commentModels.get(position).child.get(i).name);
                tv_detail_comment_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击头像，进入该用户详情
                        ActivityUtils.startOtherHomePage(context, uid);
                    }
                });
                String detail = "回复" +
                        commentModels.get(position).child.get(i).parent_name +
                        ":";// + commentModels.get(position).child.get(i).detail;
                SpannableString spannableString = new SpannableString(detail);
                spannableString.setSpan(new Clickable(commentModels.get(position).child.get(i).parent_uid), 2,
                        commentModels.get(position).child.get(i).parent_name.length() + 2,
                        Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                tv_child_comment_content.setText(spannableString);
                String msg = commentModels.get(position).child.get(i).detail;
                try {
                    new EmojiUtil().handlerEmojiTextAppend(tv_child_comment_content, msg, context);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                tv_child_comment_content.setMovementMethod(LinkMovementMethod.getInstance());

                ((CommentViewHolder) holder).linearLayout.addView(subView);
            }
        }
        ((CommentViewHolder) holder).rl_comment_item_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入评论详情
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(commentModels.get(position).coid);
                }
            }
        });
        ((CommentViewHolder) holder).rl_more_com.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入评论详情
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(commentModels.get(position).coid);
                }
            }
        });
        ((CommentViewHolder) holder).ll_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点赞
                if (itemZanClick != null) {
                    itemZanClick.itemZanClick(position, commentModels.get(position).coid);
                }
            }
        });

        final String uid = commentModels.get(position).uid;
        Glide.with(context).load(commentModels.get(position).iconurl).error(R.drawable.icon_login).into(((CommentViewHolder) holder).civ_u_pic_hot_comment);
        ((CommentViewHolder) holder).civ_u_pic_hot_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入用户详情
                ActivityUtils.startOtherHomePage(context, uid);
            }
        });
//        ((CommentViewHolder) holder).tv_comment_content.setText(commentModels.get(position).detail);
        try {
            new EmojiUtil().handlerEmojiText(((CommentViewHolder) holder).tv_comment_content, commentModels.get(position).detail, context);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ((CommentViewHolder) holder).tv_comment_count.setText("" + StringUtils.toWan(commentModels.get(position).com_time));
        ((CommentViewHolder) holder).tv_comment_name.setText(commentModels.get(position).name);
        ((CommentViewHolder) holder).tv_comment_time.setText("" + commentModels.get(position).pass_time);
        ((CommentViewHolder) holder).tv_comment_zan_count.setText(StringUtils.toWan(commentModels.get(position).praise) + "");
        ((CommentViewHolder) holder).iv_comment_zan.setImageBitmap(commentModels.get(position).if_praise == 0 ?
                BitmapUtils.getBitmap(context, R.drawable.like_un) : BitmapUtils.getBitmap(context, R.drawable.like));
    }

    public void addCommentOne(CommentModel model) {
        if (model == null) return;
        commentModels.add(model);
        notifyDataSetChanged();
    }

    public void addCommentTwo(CommentModel model, int pos) {
        if (model == null) return;
        commentModels.get(pos).child.add(model);
        notifyDataSetChanged();
    }

    public void addComment(ArrayList<CommentModel> commentModelArrayList) {
        if (commentModelArrayList == null || commentModelArrayList.isEmpty()) return;

        for (CommentModel commentModel : commentModelArrayList) {
            commentModels.add(commentModel);
        }
        notifyItemInserted(getItemCount());
        notifyDataSetChanged();
    }

    public void addComment(CommentModel commentModel) {
        if (commentModel == null) return;
        commentModels.add(0, commentModel);
        notifyItemInserted(0);
        notifyDataSetChanged();
    }

    public void zanPosition(int pos, int do_type, int praise) {
        commentModels.get(pos).if_praise = do_type;
        commentModels.get(pos).praise = praise;
        notifyItemChanged(pos);
       // notifyDataSetChanged();
    }


    public void setNameOnClick(NameOnClick nameOnClick) {
        this.nameOnClick = nameOnClick;
    }


    public void setItemZanClick(ItemZanClick itemZanClick) {
        this.itemZanClick = itemZanClick;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(String coid);
    }

    public void setSubItemRepeat(SubItemRepeat subItemRepeat) {
        this.subItemRepeat = subItemRepeat;
    }


    @Override
    public int getItemCount() {
        return commentModels == null ? 0 : commentModels.size();
    }

    public interface NameOnClick {
        void nameOnclik(String uid);
    }

    public interface ItemZanClick {
        void itemZanClick(int position, String coid);
    }

    public interface SubItemRepeat {
        void repeat(String coid, int pos, String name);
    }

    class CommentViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        LinearLayout ll_zan;
        RelativeLayout rl_comment_item_container;
        RelativeLayout rl_more_com;
        CircleImageView civ_u_pic_hot_comment;
        TextView tv_comment_name;
        TextView tv_comment_time;
        TextView tv_comment_content;
        TextView tv_comment_zan_count;
        TextView tv_comment_count;
        ImageView iv_comment_zan;

        public CommentViewHolder(View view) {
            super(view);
            linearLayout = (LinearLayout) view.findViewById(R.id.ll_sub_comment);
            rl_more_com = (RelativeLayout) view.findViewById(R.id.rl_more_com);
            ll_zan = (LinearLayout) view.findViewById(R.id.ll_zan);
            rl_comment_item_container = (RelativeLayout) view.findViewById(R.id.rl_comment_item_container);
            civ_u_pic_hot_comment = (CircleImageView) view.findViewById(R.id.civ_u_pic_hot_comment);
            tv_comment_name = (TextView) view.findViewById(R.id.tv_comment_name);
            tv_comment_time = (TextView) view.findViewById(R.id.tv_comment_time);
            tv_comment_content = (TextView) view.findViewById(R.id.tv_comment_content);
            tv_comment_zan_count = (TextView) view.findViewById(R.id.tv_comment_zan_count);
            tv_comment_count = (TextView) view.findViewById(R.id.tv_comment_count);
            iv_comment_zan = (ImageView) view.findViewById(R.id.iv_comment_zan);
        }
    }

    class Clickable extends ClickableSpan {
        private String uid;

        public Clickable(String s) {
            uid = s;
        }

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            if (nameOnClick != null)
                nameOnClick.nameOnclik(uid);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor("#80cbc4"));
        }
    }
}
